'use strict';

goog.provide('Blockly.Blocks.usb');
goog.require('Blockly.Blocks');
Blockly.Msg['USB_HUE'] = 140;

//鼠标模拟开启或关闭
Blockly.Blocks.xnr_mouse_begin_end = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Mouse.png", 20, 20, "*"))
            .appendField("USB鼠标模拟")
            .appendField(new Blockly.FieldDropdown([["开启", "begin"], ["关闭", "end"]]), "type");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->USB鼠标模拟开启或关闭\n"
            + "2.语法:\n"
            + "->开启 - void begin(void);\n"
            + "->关闭 - void end(void);\n"
            + "3.参数:无\n"
            + "4.返回值:无"
        );
        this.setHelpUrl("");
    }
};

//定义鼠标按键
Blockly.Blocks.xnr_mouse_click_location = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([["左键", "MOUSE_LEFT"], ["中键", "MOUSE_MIDDLE"], ["右键", "MOUSE_RIGHT"]]), "location_type");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "定义鼠标按键\n"
            + "左键 - MOUSE_LEFT - 1\n"
            + "右键 - MOUSE_RIGHT - 2\n"
            + "中键 - MOUSE_MIDDLE - 4\n"
            + "全部按键 - MOUSE_ALL - (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)\n"
        );
        this.setHelpUrl("");
    }
};

//操作鼠标，单击、按下、释放鼠标按键
Blockly.Blocks.xnr_mouse_click = {
    init: function () {
        this.appendValueInput("click_location")
            .setCheck(null)
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Mouse.png", 20, 20, "*"))
            .appendField(new Blockly.FieldDropdown([["点击", "click"], ["按下", "press"], ["释放", "release"]]), "click_type")
            .appendField("鼠标");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->操作鼠标，单击、按下、释放鼠标按键\n"
            + "2.语法:\n"
            + "->单击 - void click(uint8_t b = MOUSE_LEFT);\n"
            + "->按下 - void press(uint8_t b = MOUSE_LEFT);\n"
            + "->释放 - void release(uint8_t b = MOUSE_LEFT);\n"
            + "3.参数:\n"
            + "b:鼠标上的某个按键\n"
            + "4.返回值:无"
        );
        this.setHelpUrl("");
    }
};

//移动鼠标
Blockly.Blocks.xnr_mouse_move = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Mouse.png", 20, 20, "*"))
            .appendField("移动鼠标");
        this.appendValueInput("move_x")
            .setCheck(null)
            .appendField("X偏移量");
        this.appendValueInput("move_y")
            .setCheck(null)
            .appendField("Y偏移量");
        this.appendValueInput("move_wheel")
            .setCheck(null)
            .appendField("滚轮偏移量");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->移动鼠标\n"
            + "2.语法:\n"
            + "->void move(signed char x, signed char y, signed char wheel = 0);\n"
            + "3.参数:\n"
            + "->x:横坐标方向上的偏移量\n"
            + "->y:纵坐标方向上的偏移量\n"
            + "->wheel:滚轮的偏移量\n"
            + "4.返回值:无"
        );
        this.setHelpUrl("");
    }
};

//检测鼠标按键是否被按下
Blockly.Blocks.xnr_mouse_isPressed = {
    init: function () {
        this.appendValueInput("type")
            .setCheck(null)
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Mouse.png", 20, 20, "*"))
            .appendField("鼠标");
        this.appendDummyInput()
            .appendField("被按下？");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->检测鼠标某个按键是否被按下，返回数据的类型为boolean\n"
            + "2.语法:\n"
            + "->bool isPressed(uint8_t b = MOUSE_LEFT);\n"
            + "3.参数:\n"
            + "->b:鼠标上的某个按键\n"
            + "4.返回值:boolean型数据(按下|释放)"
        );
        this.setHelpUrl("");
    }
};

//键盘模拟开启或关闭
Blockly.Blocks.xnr_keyboard_begin_end = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Keyboard.png", 20, 20, "*"))
            .appendField("USB键盘模拟")
            .appendField(new Blockly.FieldDropdown([["开启", "begin"], ["关闭", "end"]]), "type");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->USB键盘模拟开启或关闭\n"
            + "2.语法:\n"
            + "->开启 - void begin(void);\n"
            + "->关闭 - void end(void);\n"
            + "3.参数:无\n"
            + "4.返回值:无"
        );
        this.setHelpUrl("");
    }
};

//定义键盘的按键类型
Blockly.Blocks.xnr_keyboard_key = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([["左Ctrl键", "KEY_LEFT_CTRL"], ["左Shift键", "KEY_LEFT_SHIFT"], ["左ALT键", "KEY_LEFT_ALT"], ["左GUI键", "KEY_LEFT_GUI"], ["右Ctrl键", "KEY_RIGHT_CTRL"], ["右Shift键", "KEY_RIGHT_SHIFT"], ["右ALT键", "KEY_RIGHT_ALT"], ["右GUI键", "KEY_RIGHT_GUI"], ["方向键上", "KEY_UP_ARROW"], ["方向键下", "KEY_DOWN_ARROW"], ["方向键左", "KEY_LEFT_ARROW"], ["方向键右", "KEY_RIGHT_ARROW"], ["退格键", "KEY_BACKSPACE"], ["Tab键", "KEY_TAB"], ["Return(Enter)键", "KEY_RETURN"], ["Esc键", "KEY_ESC"], ["INSERT键", "KEY_INSERT"], ["Delete键", "KEY_DELETE"], ["PageUp键", "KEY_PAGE_UP"], ["PageDown键", "KEY_PAGE_DOWN"], ["Home键", "KEY_HOME"], ["End键", "KEY_END"], ["CapsLock键", "KEY_CAPS_LOCK"], ["F1键", "KEY_F1"], ["F2键", "KEY_F2"], ["F3键", "KEY_F3"], ["F4键", "KEY_F4"], ["F5键", "KEY_F5"], ["F6键", "KEY_F6"], ["F7键", "KEY_F7"], ["F8键", "KEY_F8"], ["F9键", "KEY_F9"], ["F10键", "KEY_F10"], ["F11键", "KEY_F11"], ["F12键", "KEY_F12"]]), "key_type");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip("定义键盘的按键类型");
        this.setHelpUrl("");
    }
};

//操作键盘，按下或释放某个按键
Blockly.Blocks.xnr_keyboard_press = {
    init: function () {
        this.appendValueInput("key")
            .setCheck(null)
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Keyboard.png", 20, 20, "*"))
            .appendField(new Blockly.FieldDropdown([["按下", "press"], ["释放", "release"]]), "type")
            .appendField("键盘上");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->操作键盘，按下或释放某个按键\n"
            + "2.语法:\n"
            + "->按下 - size_t press(uint8_t k);\n"
            + "->释放 - size_t release(uint8_t k);\n"
            + "3.参数:\n"
            + "->k:键盘上的某个按键\n"
            + "4.返回值:size_t型数据(不使用返回值)"
        );
        this.setHelpUrl("");
    }
};

//操作键盘，按下或释放某个按键，返回结果
Blockly.Blocks.xnr_keyboard_press_return = {
    init: function () {
        this.appendValueInput("key")
            .setCheck(null)
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Keyboard.png", 20, 20, "*"))
            .appendField(new Blockly.FieldDropdown([["按下", "press"], ["释放", "release"]]), "type")
            .appendField("键盘上");
        //this.appendDummyInput()  
        //    .appendField("返回结果");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->操作键盘，按下或释放某个按键，返回数据的类型为size_t(2字节)\n"
            + "2.语法:\n"
            + "->按下 - size_t press(uint8_t k);\n"
            + "->释放 - size_t release(uint8_t k);\n"
            + "3.参数:\n"
            + "->k:键盘上的某个按键\n"
            + "4.返回值:size_t型数据\n"
            + "->1 - 成功\n"
            + "->0 - 失败"
        );
        this.setHelpUrl("");
    }
};

//操作键盘，释放键盘上所有按键
Blockly.Blocks.xnr_keyboard_releaseAll = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Keyboard.png", 20, 20, "*"))
            .appendField("释放键盘上所有按键");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->操作键盘，释放键盘上所有按键\n"
            + "2.语法:\n"
            + "->void releaseAll(void);\n"
            + "3.参数:无\n"
            + "4.返回值:无"
        );
        this.setHelpUrl("");
    }
};

//键盘向电脑发送字符
Blockly.Blocks.xnr_keyboard_write = {
    init: function () {
        this.appendValueInput("write_data")
            .setCheck(null)
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Keyboard.png", 20, 20, "*"))
            .appendField("键盘发送字符");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->键盘向电脑发送字符\n"
            + "2.语法:\n"
            + "->size_t write(uint8_t k);\n"
            + "3.参数:\n"
            + "->k:键盘上的某个按键\n"
            + "4.返回值:size_t型数据(不使用返回值)"
        );
        this.setHelpUrl("");
    }
};

//键盘向电脑发送字符，返回结果
Blockly.Blocks.xnr_keyboard_write_return = {
    init: function () {
        this.appendValueInput("write_data")
            .setCheck(null)
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Keyboard.png", 20, 20, "*"))
            .appendField("键盘发送字符");
        //this.appendDummyInput()  
        //    .appendField("返回结果");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->键盘向电脑发送字符，返回数据的类型为size_t(2字节)\n\n"
            + "2.语法:\n"
            + "->size_t write(uint8_t k);\n"
            + "3.参数:\n"
            + "->k:键盘上的某个按键\n"
            + "4.返回值:size_t型数据\n"
            + "->1 - 成功\n"
            + "->0 - 失败"
        );
        this.setHelpUrl("");
    }
};

//键盘向电脑发送字符串
Blockly.Blocks.xnr_keyboard_print = {
    init: function () {
        this.appendValueInput("print_data")
            .setCheck(null)
            .appendField(new Blockly.FieldImage("./media/blocks_icon/USB_Keyboard.png", 20, 20, "*"))
            .appendField("键盘发送")
            .appendField(new Blockly.FieldDropdown([["字符串", "print"], ["字符串(自动换行)", "println"]]), "print_type");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(Blockly.Msg['USB_HUE']);
        this.setTooltip(
            "1.功能:\n"
            + "->键盘向电脑发送字符串\n"
            + "2.语法:\n"
            + "->发送字符串 - print(String data);\n"
            + "->发送字符串(自动换行) - println(String data);\n"
            + "3.参数:\n"
            + "data:键盘上的某几个按键的组合"
        );
        this.setHelpUrl("");
    }
};

Blockly.Blocks.xnr_setup= {
    init: function() {
    this.appendDummyInput()
        .appendField("  初始化：void setup()");
    this.appendStatementInput("xyt")
        .setCheck(null);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(Blockly.Msg['USB_HUE']);
    this.setTooltip("");
    this.setHelpUrl("");
    }
};

Blockly.Blocks.xnr_serialBT_SerialBT= {
    init: function() { 
    this.appendDummyInput()
        .appendField("Pico蓝牙是否开启成功?");
    this.setOutput(true, null);
    this.setColour(Blockly.Msg['USB_HUE']);
    this.setTooltip("");
    this.setHelpUrl("");
    }
  };

Blockly.Blocks.xnr_serialBT_available= {
    init: function() { 
    this.appendDummyInput()
        .appendField("Pico蓝牙有数据可读吗?");
    this.setOutput(true, null);
    this.setColour(Blockly.Msg['USB_HUE']);
    this.setTooltip("");
    this.setHelpUrl("");
    }
};

Blockly.Blocks.xnr_serialBT_read= {
    init: function() { 
    this.appendDummyInput()
        .appendField("Pico蓝牙数据");
    this.setOutput(true, null);
    this.setColour(Blockly.Msg['USB_HUE']);
    this.setTooltip("");
    this.setHelpUrl("");
    }
  };

  Blockly.Blocks.xnr_serialBT_write= {
    init: function() { 
    this.appendValueInput("data")
        .setCheck(null)
        .appendField("Pico蓝牙发送数据");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(Blockly.Msg['USB_HUE']);
    this.setTooltip("");
    this.setHelpUrl("");
    }
  };
  
  Blockly.Blocks.xnr_serialBT_init= {
    init: function() { 
    this.appendValueInput("value")
        .setCheck(null)
        .appendField("初始化Pico蓝牙   通信波特率");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(Blockly.Msg['USB_HUE']);
    this.setTooltip("");
    this.setHelpUrl("");
    }
  };